{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "from tqdm import tqdm"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Load Citeseer Dataset"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "from graphgallery.datasets import Planetoid\n",
    "data = Planetoid('citeseer', root=\"~/GraphData/datasets/\", verbose=False)\n",
    "graph = data.graph\n",
    "splits = data.split_nodes()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# TensorFlow Backend"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "TensorFlow 2.1.2 Backend"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from graphgallery import set_backend\n",
    "set_backend('tensorflow')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 10/10 [00:13<00:00,  1.31s/it]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Final results: 70.61%±1.60%\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\n"
     ]
    }
   ],
   "source": [
    "from graphgallery.gallery import GCN\n",
    "\n",
    "accs = []\n",
    "\n",
    "for seed in tqdm(range(10)):\n",
    "    trainer = GCN(graph, device=\"gpu\", seed=42+seed).process(attr_transform=\"normalize_attr\").build()\n",
    "    his = trainer.train(splits.train_nodes, splits.val_nodes, verbose=0, epochs=100)\n",
    "    results = trainer.test(splits.test_nodes, verbose=0) \n",
    "    accs.append(results.accuracy)\n",
    "    \n",
    "print(f\"Final results: {np.mean(accs):.2%}±{np.std(accs):.2%}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 10/10 [00:06<00:00,  1.53it/s]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Final results: 72.70%±0.69%\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\n"
     ]
    }
   ],
   "source": [
    "from graphgallery.gallery import SGC\n",
    "\n",
    "accs = []\n",
    "\n",
    "for seed in tqdm(range(10)):\n",
    "    trainer = SGC(graph, device=\"gpu\", seed=42+seed).process(attr_transform=\"normalize_attr\").build()\n",
    "    his = trainer.train(splits.train_nodes, splits.val_nodes, verbose=0, epochs=100)\n",
    "    results = trainer.test(splits.test_nodes, verbose=0) \n",
    "    accs.append(results.accuracy)\n",
    "    \n",
    "print(f\"Final results: {np.mean(accs):.2%}±{np.std(accs):.2%}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "  0%|          | 0/10 [00:00<?, ?it/s]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "WARNING:tensorflow:Large dropout rate: 0.6 (>0.5). In TensorFlow 2.x, dropout() uses dropout rate instead of keep_prob. Please ensure that this is intended.\n",
      "WARNING:tensorflow:Large dropout rate: 0.6 (>0.5). In TensorFlow 2.x, dropout() uses dropout rate instead of keep_prob. Please ensure that this is intended.\n",
      "WARNING:tensorflow:Large dropout rate: 0.6 (>0.5). In TensorFlow 2.x, dropout() uses dropout rate instead of keep_prob. Please ensure that this is intended.\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      " 10%|█         | 1/10 [00:26<03:57, 26.43s/it]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "WARNING:tensorflow:Large dropout rate: 0.6 (>0.5). In TensorFlow 2.x, dropout() uses dropout rate instead of keep_prob. Please ensure that this is intended.\n",
      "WARNING:tensorflow:Large dropout rate: 0.6 (>0.5). In TensorFlow 2.x, dropout() uses dropout rate instead of keep_prob. Please ensure that this is intended.\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 10/10 [03:56<00:00, 23.68s/it]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Final results: 72.19%±0.39%\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\n"
     ]
    }
   ],
   "source": [
    "from graphgallery.gallery import GAT\n",
    "\n",
    "accs = []\n",
    "\n",
    "for seed in tqdm(range(10)):\n",
    "    trainer = GAT(graph, device=\"gpu\", seed=42+seed).process(attr_transform=\"normalize_attr\").build()\n",
    "    his = trainer.train(splits.train_nodes, splits.val_nodes, verbose=0, epochs=200)\n",
    "    results = trainer.test(splits.test_nodes, verbose=0) \n",
    "    accs.append(results.accuracy)\n",
    "    \n",
    "print(f\"Final results: {np.mean(accs):.2%}±{np.std(accs):.2%}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# PyTorch Backend"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "PyTorch 1.6.0+cu101 Backend"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from graphgallery import set_backend\n",
    "set_backend('pytorch')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 10/10 [00:22<00:00,  2.23s/it]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Final results: 70.46%±0.89%\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\n"
     ]
    }
   ],
   "source": [
    "from graphgallery.gallery import GCN\n",
    "\n",
    "accs = []\n",
    "\n",
    "for seed in tqdm(range(10)):\n",
    "    trainer = GCN(graph, device=\"gpu\", seed=42+seed).process(attr_transform=\"normalize_attr\").build()\n",
    "    his = trainer.train(splits.train_nodes, splits.val_nodes, verbose=0, epochs=200)\n",
    "    results = trainer.test(splits.test_nodes, verbose=0) \n",
    "    accs.append(results.accuracy)\n",
    "    \n",
    "print(f\"Final results: {np.mean(accs):.2%}±{np.std(accs):.2%}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 10/10 [00:03<00:00,  3.07it/s]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Final results: 71.75%±0.15%\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\n"
     ]
    }
   ],
   "source": [
    "from graphgallery.gallery import SGC\n",
    "\n",
    "accs = []\n",
    "\n",
    "for seed in tqdm(range(10)):\n",
    "    trainer = SGC(graph, device=\"gpu\", seed=42+seed).process(attr_transform=\"normalize_attr\").build()\n",
    "    his = trainer.train(splits.train_nodes, splits.val_nodes, verbose=0, epochs=100)\n",
    "    results = trainer.test(splits.test_nodes, verbose=0) \n",
    "    accs.append(results.accuracy)\n",
    "    \n",
    "print(f\"Final results: {np.mean(accs):.2%}±{np.std(accs):.2%}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 10/10 [02:32<00:00, 15.20s/it]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Final results: 71.63%±0.45%\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\n"
     ]
    }
   ],
   "source": [
    "from graphgallery.gallery import GAT\n",
    "\n",
    "accs = []\n",
    "\n",
    "for seed in tqdm(range(10)):\n",
    "    trainer = GAT(graph, device=\"gpu\", seed=42+seed).process(attr_transform=\"normalize_attr\").build()\n",
    "    his = trainer.train(splits.train_nodes, splits.val_nodes, verbose=0, epochs=200)\n",
    "    results = trainer.test(splits.test_nodes, verbose=0) \n",
    "    accs.append(results.accuracy)\n",
    "    \n",
    "print(f\"Final results: {np.mean(accs):.2%}±{np.std(accs):.2%}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# PyG Backend"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "PyTorch Geometric 1.6.1 (PyTorch 1.6.0+cu101) Backend"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from graphgallery import set_backend\n",
    "set_backend('pyg')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "import graphgallery as gg"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Using backend: pytorch\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "DGL PyTorch 0.5.2 (PyTorch 1.6.0+cu101) Backend"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "gg.backend(\"DGL\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 10/10 [00:07<00:00,  1.30it/s]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Final results: 70.17%±1.01%\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\n"
     ]
    }
   ],
   "source": [
    "from graphgallery.gallery import GCN\n",
    "\n",
    "accs = []\n",
    "\n",
    "for seed in tqdm(range(10)):\n",
    "    trainer = GCN(graph, device=\"gpu\", seed=42+seed).process(attr_transform=\"normalize_attr\").build()\n",
    "    his = trainer.train(splits.train_nodes, splits.val_nodes, verbose=0, epochs=200)\n",
    "    results = trainer.test(splits.test_nodes, verbose=0) \n",
    "    accs.append(results.accuracy)\n",
    "    \n",
    "print(f\"Final results: {np.mean(accs):.2%}±{np.std(accs):.2%}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 10/10 [00:04<00:00,  2.15it/s]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Final results: 71.96%±0.05%\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\n"
     ]
    }
   ],
   "source": [
    "from graphgallery.gallery import SGC\n",
    "\n",
    "accs = []\n",
    "\n",
    "for seed in tqdm(range(10)):\n",
    "    trainer = SGC(graph, device=\"gpu\", seed=42+seed).process(attr_transform=\"normalize_attr\").build()\n",
    "    his = trainer.train(splits.train_nodes, splits.val_nodes, verbose=0, epochs=200)\n",
    "    results = trainer.test(splits.test_nodes, verbose=0) \n",
    "    accs.append(results.accuracy)\n",
    "    \n",
    "print(f\"Final results: {np.mean(accs):.2%}±{np.std(accs):.2%}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 10/10 [00:40<00:00,  4.01s/it]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Final results: 71.55%±1.18%\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\n"
     ]
    }
   ],
   "source": [
    "from graphgallery.gallery import GAT\n",
    "\n",
    "accs = []\n",
    "\n",
    "for seed in tqdm(range(10)):\n",
    "    trainer = GAT(graph, device=\"gpu\", seed=42+seed).process(attr_transform=\"normalize_attr\").build()\n",
    "    his = trainer.train(splits.train_nodes, splits.val_nodes, verbose=0, epochs=200)\n",
    "    results = trainer.test(splits.test_nodes, verbose=0) \n",
    "    accs.append(results.accuracy)\n",
    "    \n",
    "print(f\"Final results: {np.mean(accs):.2%}±{np.std(accs):.2%}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# DGL (PyTorch) Backend"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DGL PyTorch 0.5.2 (PyTorch 1.6.0+cu101) Backend"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from graphgallery import set_backend\n",
    "set_backend('dgl')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 10/10 [00:52<00:00,  5.25s/it]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Final results: 70.76%±1.24%\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\n"
     ]
    }
   ],
   "source": [
    "from graphgallery.gallery import GCN\n",
    "\n",
    "accs = []\n",
    "\n",
    "for seed in tqdm(range(10)):\n",
    "    trainer = GCN(graph, device=\"gpu\", seed=42+seed).process(attr_transform=\"normalize_attr\").build()\n",
    "    his = trainer.train(splits.train_nodes, splits.val_nodes, verbose=0, epochs=200)\n",
    "    results = trainer.test(splits.test_nodes, verbose=0) \n",
    "    accs.append(results.accuracy)\n",
    "    \n",
    "print(f\"Final results: {np.mean(accs):.2%}±{np.std(accs):.2%}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 10/10 [00:10<00:00,  1.02s/it]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Final results: 71.98%±0.07%\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\n"
     ]
    }
   ],
   "source": [
    "from graphgallery.gallery import SGC\n",
    "\n",
    "accs = []\n",
    "\n",
    "for seed in tqdm(range(10)):\n",
    "    trainer = SGC(graph, device=\"gpu\", seed=42+seed).process(attr_transform=\"normalize_attr\").build()\n",
    "    his = trainer.train(splits.train_nodes, splits.val_nodes, verbose=0, epochs=100)\n",
    "    results = trainer.test(splits.test_nodes, verbose=0) \n",
    "    accs.append(results.accuracy)\n",
    "    \n",
    "print(f\"Final results: {np.mean(accs):.2%}±{np.std(accs):.2%}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 10/10 [00:31<00:00,  3.11s/it]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Final results: 71.03%±0.48%\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\n"
     ]
    }
   ],
   "source": [
    "from graphgallery.gallery import GAT\n",
    "\n",
    "accs = []\n",
    "\n",
    "for seed in tqdm(range(10)):\n",
    "    trainer = GAT(graph, device=\"gpu\", seed=42+seed).process(attr_transform=\"normalize_attr\").build()\n",
    "    his = trainer.train(splits.train_nodes, splits.val_nodes, verbose=0, epochs=200)\n",
    "    results = trainer.test(splits.test_nodes, verbose=0) \n",
    "    accs.append(results.accuracy)\n",
    "    \n",
    "print(f\"Final results: {np.mean(accs):.2%}±{np.std(accs):.2%}\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
